package com.imufe.chemicalenterprisedangerwarning.util;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imufe.chemicalenterprisedangerwarning.entity.vo.EnterpriseTypeVO;
import org.springframework.beans.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * @author BaiBan
 * @since 2022/9/22
 */
public class ListUtils {

    /**
     * POJO 之间的集合转换
     */
    public static <E, T> List<T> copyList(List<E> list, Class<T> clazz) {
        return list.stream().map((e) -> {
            try {
                T t = clazz.getDeclaredConstructor().newInstance();
                if(e != null)
                    BeanUtils.copyProperties(e, t);
                return t;
            } catch (InstantiationException | IllegalAccessException | InvocationTargetException |
                     NoSuchMethodException ex) {
                throw new RuntimeException(ex);
            }
        }).toList();
    }

    /**
     * Page拷贝
     */
    public static <E, T> Page<T> copyPage(Page<E> sourcePage, Class<T> clazz) {
        Page<T> page = new Page<>();
        BeanUtils.copyProperties(sourcePage, page);
        return page;
    }
}
